<template>
  <view>
    <view class="search-box">
      <uni-search-bar @confirm="search" @input="input" :radius="18" cancelButton="none"></uni-search-bar>
    </view>
    <view class="suggest-list" v-if="searchResults.length!=0">
      <view class="suggest-list-item" v-for="(item,index) in searchResults" :key="index"
        @click="gotoDetail(item.goods_id)">
        <text class="goods_name">{{item.goods_name}}</text>
        <uni-icons color="#c0c4cc" size="18" type="arrowright" />
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-if="searchResults.length==0">
      <!-- 标题 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons color="#c0c4cc" size="18" type="trash" @click="clearHistory()"/>
      </view>
      <!-- 列表区 -->
      <view class="history-list"  >
        <view @click="gotoGoodsList(item)" class="history-list-item" v-for="(item,index) in historys" :key="index">
          <uni-tag :text="item"></uni-tag>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timeout: null,
        searchResults: [],
        historyList: []
      }
    },
    onLoad() {
      this.historyList=JSON.parse(uni.getStorageSync('history')||'[]')
    }
    ,
    computed:{
      historys(){
        return [...this.historyList].reverse()
      }
    },
    methods: {
      input(value) {
        clearTimeout(this.timeout)
        this.timeout = setTimeout(() => {
          this.getSearchResults(value)
          this.saveSearchHistory(value)
        }, 500)
      },
      async getSearchResults(value) {
        if (value == '') {
          this.searchResults = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: value
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.searchResults = res.message
      },
      gotoDetail(id) {
        uni.navigateTo({
          url: '/subpackges/goods_detail/goods_detail?goods.id=' + id
        })
      },
      saveSearchHistory(value){
        
        let set=new Set(this.historyList)
        set.delete(value)
        set.add(value)
     
        this.historyList=Array.from(set)
        uni.setStorageSync('history',JSON.stringify(this.historyList))
      },
      clearHistory(){
      console.log(11)
      this.historyList=[]
      uni.setStorageSync('history','[]')
    },
    gotoGoodsList(item){
      uni.navigateTo({
        url:'/subpackges/goods_list/goods_list?query='+item
      })
    }
    }
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .suggest-list {
    padding: 0 5px;

    .suggest-list-item {
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      align-items: center;
      margin: 5px 0;
      border-bottom: 2px solid #efefef;

      .goods_name {
        text-overflow: ellipsis;
        overflow: hidden;
        margin-right: 3px;
        white-space: nowrap;
      }
    }
  }
  .history-box {
    padding: 0 5px;
   .history-title {
     display: flex;
     align-items: center;
     height: 40px;
     justify-content: space-between;
     border-bottom: 1px solie #efefef;
   } 
   
   .history-list {
     display: flex;
       flex-wrap: wrap;
     .history-list-item {
       margin: 5px;
     }
   }
  }
</style>